package com.laisheng.interview.service.impl;

import com.laisheng.interview.model.ResumeData;
import com.laisheng.interview.service.ResumeParseService;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 简历解析服务实现类
 */
@Service
public class ResumeParseServiceImpl implements ResumeParseService {
    /**
     * 解析PDF格式简历
     * @param file 上传的PDF文件
     * @return 解析后的简历数据
     * @throws Exception 解析异常
     */
    @Override
    public ResumeData parsePdfResume(MultipartFile file) throws Exception {
        try (PDDocument document = PDDocument.load(file.getInputStream())) {
            PDFTextStripper stripper = new PDFTextStripper();
            String text = stripper.getText(document);
            return parseTextResume(text);
        } catch (IOException e) {
            throw new Exception("PDF解析失败: " + e.getMessage());
        }
    }

    @Override
    public ResumeData parseTextResume(String text) {
        ResumeData resumeData = new ResumeData();
        
        // 提取姓名
        resumeData.setName(extractName(text));
        
        // 提取电话
        resumeData.setPhone(extractPhone(text));
        
        // 提取邮箱
        resumeData.setEmail(extractEmail(text));
        
        // 提取教育背景
        resumeData.setEducation(extractEducation(text));
        
        // 提取专业
        resumeData.setMajor(extractMajor(text));
        
        // 提取出生日期
        resumeData.setBirthDate(extractBirthDate(text));
        
        // 提取性别
        resumeData.setGender(extractGender(text));
        
        // 提取期望薪资
        resumeData.setExpectedSalary(extractExpectedSalary(text));
        
        // 提取工作经验
        resumeData.setWorkExperience(extractWorkExperience(text));
        
        // 提取技能
        resumeData.setSkills(extractSkills(text));
        
        // 提取地址
        resumeData.setAddress(extractAddress(text));
        
        // 提取身份证号
        resumeData.setIdNumber(extractIdNumber(text));
        
        // 提取婚姻状况
        resumeData.setMaritalStatus(extractMaritalStatus(text));
        
        // 提取身高
        resumeData.setHeight(extractHeight(text));
        
        // 提取民族
        resumeData.setNationality(extractNationality(text));
        
        return resumeData;
    }
    
    /**
     * 从文本中提取姓名
     * @param text 简历文本
     * @return 提取的姓名
     */
    private String extractName(String text) {
        // 常见的姓名模式
        Pattern pattern = Pattern.compile("姓名[：:]*([\\u4e00-\\u9fa5]{2,4})");
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            return matcher.group(1);
        }
        
        // 如果没有找到"姓名"标签，尝试其他模式
        pattern = Pattern.compile("([\\u4e00-\\u9fa5]{2,4})[\\s\\n]*简历");
        matcher = pattern.matcher(text);
        if (matcher.find()) {
            return matcher.group(1);
        }
        
        return "";
    }
    
    private String extractPhone(String text) {
        Pattern pattern = Pattern.compile("(1[3-9]\\d{9})");
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return "";
    }
    
    private String extractEmail(String text) {
        Pattern pattern = Pattern.compile("([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,})");
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return "";
    }
    
    private String extractEducation(String text) {
        Pattern pattern = Pattern.compile("学历[：:]*([\\u4e00-\\u9fa5]+)");
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            return matcher.group(1);
        }
        
        // 尝试匹配教育经历
        pattern = Pattern.compile("([\\u4e00-\\u9fa5]+)(?:大学|学院|学校)");
        matcher = pattern.matcher(text);
        if (matcher.find()) {
            String school = matcher.group(1);
            if (text.contains("本科")) return "本科";
            if (text.contains("硕士")) return "硕士";
            if (text.contains("博士")) return "博士";
            if (text.contains("专科")) return "专科";
        }
        
        return "";
    }
    
    private String extractMajor(String text) {
        Pattern pattern = Pattern.compile("专业[：:]*([\\u4e00-\\u9fa5]+)");
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return "";
    }
    
    private String extractBirthDate(String text) {
        Pattern pattern = Pattern.compile("出生[：:]*([\\d]{4}[-/年][\\d]{1,2}[-/月][\\d]{1,2}[日]?)");
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return "";
    }
    
    private String extractGender(String text) {
        if (text.contains("男")) return "男";
        if (text.contains("女")) return "女";
        return "";
    }
    
    private String extractExpectedSalary(String text) {
        Pattern pattern = Pattern.compile("期望[薪薪资][：:]*([\\d]+[-~到][\\d]+|[\\d]+)");
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return "";
    }
    
    private String extractWorkExperience(String text) {
        // 提取工作经验部分
        Pattern pattern = Pattern.compile("工作经历[：:]*([\\s\\S]*?)(?=教育经历|技能|联系方式|$)");
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            return matcher.group(1).trim();
        }
        return "";
    }
    
    private String extractSkills(String text) {
        Pattern pattern = Pattern.compile("技能[：:]*([\\s\\S]*?)(?=工作经历|教育经历|联系方式|$)");
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            return matcher.group(1).trim();
        }
        return "";
    }
    
    private String extractAddress(String text) {
        Pattern pattern = Pattern.compile("地址[：:]*([\\u4e00-\\u9fa5]+)");
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return "";
    }
    
    private String extractIdNumber(String text) {
        Pattern pattern = Pattern.compile("([\\d]{17}[\\dXx])");
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return "";
    }
    
    private String extractMaritalStatus(String text) {
        if (text.contains("未婚")) return "未婚";
        if (text.contains("已婚")) return "已婚";
        if (text.contains("离异")) return "离异";
        return "";
    }
    
    private String extractHeight(String text) {
        Pattern pattern = Pattern.compile("身高[：:]*([\\d]+)");
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return "";
    }
    
    private String extractNationality(String text) {
        Pattern pattern = Pattern.compile("民族[：:]*([\\u4e00-\\u9fa5]+)");
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return "";
    }
}